package com.sanji.sjmallmodule.permission;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;

import com.sanji.sjmallapp.BaseActivity;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallutils.PermissionsChecker;


/**
 * 权限获取页面
 * Created by zsf on 17-4-15.
 */

public class PermissionsActivity extends BaseActivity {

    public static final int PERMISSION_GRANTED = 0;//授权
    public static final int PERMISSION_DENIED = 1;//拒绝

    private static final int PERMISSION_REQUEST_CODE = 0;//系统权限管理页面的参数
    private static final String EXTRA_PERMISSIONS = "com.sanji.sjmallapp.permission.extra_permission";//权限参数
    private static final String PACKAGE_URL_SCHEME = "package:";

    private PermissionsChecker mChecker;//权限检测器
    private boolean isRequireCheck;//是否需要系统权限检测

    //启动但前权限页面的公开接口
    public static void startActivityForResult(Activity activity,int requestCode,String... permissions){
        Intent intent = new Intent(activity,PermissionsActivity.class);
        intent.putExtra(EXTRA_PERMISSIONS,permissions);
        ActivityCompat.startActivityForResult(activity,intent,requestCode,null);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getIntent() == null || !getIntent().hasExtra(EXTRA_PERMISSIONS)){
            throw new RuntimeException("PermissionsActivity需要使用静态startActivityForResult方法启动");
        }
        setContentView(R.layout.activity_permissions);

        mChecker = new PermissionsChecker(this);
        isRequireCheck = true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isRequireCheck){
            String[] permissions = getPermissions();
            if (mChecker.lacksPermissions(permissions)){
                requestPermissions(permissions);// 请求权限
            }else{
                allPermissionsGranted();
            }
        }else{
            isRequireCheck = true;
        }
    }


    // 返回传递的权限参数
    private String[] getPermissions(){
        return getIntent().getStringArrayExtra(EXTRA_PERMISSIONS);
    }

    //请求权限兼容低版本
    private void requestPermissions(String... permissions){
        ActivityCompat.requestPermissions(this,permissions,PERMISSION_REQUEST_CODE);
    }

    //全部权限均已获取
    private void allPermissionsGranted(){
        setResult(PERMISSION_GRANTED);
        finish();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if(requestCode == PERMISSION_REQUEST_CODE && hasAllPermissionsGranted(grantResults)){
            isRequireCheck = true;
            allPermissionsGranted();
        }else{
            isRequireCheck = false;
            showMissingPermissionDialog();
        }
    }


    //含有全部的权限
    private boolean hasAllPermissionsGranted(@NonNull int[] grantResults){
        for (int grantResult : grantResults){
            if (grantResult == PackageManager.PERMISSION_DENIED){
                return false;
            }
        }
        return true;
    }

    //显示缺失权限提示
    private void showMissingPermissionDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(PermissionsActivity.this);
        builder.setTitle(R.string.help);
        builder.setMessage(R.string.string_help_text);
        builder.setNegativeButton(R.string.quit, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                setResult(PERMISSION_DENIED);
                finishActivity();
            }
        });
        builder.setPositiveButton(R.string.settings, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                startAppSettings();
            }
        });
        builder.show();

    }

    // 启动应用的设置
    private void startAppSettings(){
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse(PACKAGE_URL_SCHEME + getPackageName()));
        startActivity(intent);
    }
}
